package com.jsf.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jsf.entity.*;
import com.jsf.service.IMemberService;
import com.jsf.service.INewsService;
import com.jsf.service.ISaleChanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jsf
 * @since 2022-02-21
 */
@RestController
@RequestMapping("/saleChance")
public class SaleChanceController {
    @Autowired
    private ISaleChanceService ss;
    @Autowired
    private IMemberService ms;
    @GetMapping("/selectAll")
    public List<SaleChance> selectAll(){
        return ss.list();
    }

    @GetMapping("/selectMyChance")
    public List<SaleChance> selectMyChance(HttpSession session){
        QueryWrapper<SaleChance> wrapper=new QueryWrapper<>();
        Emp user = (Emp) session.getAttribute("user");
        wrapper.eq("saleman",user.getId());
        return ss.list();
    }

    @PostMapping("/add")
    public void add(@RequestBody SaleChance saleChance){
        saleChance.setCreateTime(LocalDate.now());
        saleChance.setMessageState(1);
        ss.save(saleChance);
    }
    @DeleteMapping("/delete/{id}")
    public void delete(@PathVariable("id") int id){
        ss.removeById(id);
    }

    @PutMapping("/updateStateById")
    public void updateState(@RequestBody SaleChance saleChance){
        if(saleChance.getMessageState()!=null&&saleChance.getMessageState()==2){
            SaleChance sc = ss.getById(saleChance.getId());
            Member member=new Member();
            member.setName(sc.getLinkman());
            member.setSaleman(sc.getSaleman());
            member.setTel(sc.getTel());
            ms.save(member);
        }
        ss.updateById(saleChance);
    }

}

